package com.atguigu.ucenterservice.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.queryVo.UcenterMemberOrderVo;
import com.atguigu.ucenterservice.entity.UcenterMember;
import com.atguigu.ucenterservice.entity.vo.RegisterVo;
import com.atguigu.ucenterservice.service.UcenterMemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author thomasyang
 * @since 2023-12-18
 */

@CrossOrigin
@RestController
@RequestMapping("/educenter/member")
public class UcenterMemberController {


    @Autowired
    private UcenterMemberService memberService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    //登录
    @ApiOperation("登录")
    @PostMapping("login")
    public R login(@RequestBody UcenterMember member){
        //member对象封装手机号和密码
        //调用service方法实现登录
        //返回token值，使用jwt生成

        String token =  memberService.login(member);

        return R.ok().data("token", token);

    }

    @ApiOperation("注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){

         Boolean register =  memberService.register(registerVo);

         return R.flagCheck(register);
    }


    //根据token获取用户信息
    @ApiOperation("根据手机号获取用户信息")
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){

        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
//        String jwtToken = JwtUtils.getJwtToken(request);
//        System.out.println("jwttoken"+ jwtToken);
        String Id = JwtUtils.getMemberIdByJwtToken(request);
//        System.out.println("id, "+ Id);
        //查询数据库根据用户id获取用户信息
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("id", Id);
        UcenterMember member = memberService.getOne(wrapper);
//        System.out.println("userInfo, "+ member);
        return R.ok().data("userInfo", member);

    }

    //根据用户id获取用户信息
    @ApiOperation(value = "根据用户id获取用户信息")
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrderVo getUserInfoOrder(@PathVariable String id ){

        UcenterMember member = memberService.getById(id);

        UcenterMemberOrderVo ucenterMember = new UcenterMemberOrderVo();
        BeanUtils.copyProperties(member,ucenterMember);

        return ucenterMember;
    }

    //查询某一天注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){

        Integer count =  memberService.countRegisterDay(day);
        return R.ok().data("countRegister", count);
    }

}

